<template>
  <div>
    <div>
      <el-button type="warning" @click="dialogVisible = true"
        >+ 添加资源</el-button
      >
    </div>
    <div></div>
    <!--  -->
    <div align="left">
      <el-dialog :visible.sync="dialogVisible" :before-close="handleClose">
        <div>
          菜单名称:
          <el-input class="top1" v-model="name"></el-input>
        </div>
        <!--  -->
        <div>
          菜单类型:
          <el-button :type="status == 1 ? 'primary' : ''" @click="urlPost"
            >接口权限</el-button
          >
          <el-button :type="status == 2 ? 'primary' : ''" @click="post"
            >页面</el-button
          >
        </div>
        <!--  -->
        <div>
          菜单路径:
          <el-input v-if="status == 2" placeholder="http://" class="top1" v-model="url"></el-input>
          <!-- 下拉框 -->
          <el-select v-if="status == 1" v-model="url" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
          <!--  -->
        </div>
        <!--  -->
        <div>
          <el-radio v-model="radio" label="0">菜单</el-radio>&emsp;&emsp;
          <el-radio v-model="radio" label="1">资源</el-radio>
        </div>
        <!--  -->
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="add">保 存</el-button>
        </span>
      </el-dialog>
    </div>
    <!--  -->
    <div></div>
    <!--  -->
  </div>
</template>

<script>
export default {
  name: "",
  data() {
    return {
      dialogVisible: false,
      name: "",
      radio: "1",
      status: 1,
      url: "",
      options: [
        {
          value: "admin/rbacm/add/role",
          label: "admin/rbacm/add/role",
        },
        {
          value: "admin/rbacm/add/resource",
          label: "admin/rbacm/add/resource",
        },
        {
          value: "admin/rbacm/add/role/resource",
          label: "admin/rbacm/add/role/resource",
        },
      ],
    };
  },
  props: {},

  components: {},

  created() {},

  mounted() {},

  methods: {
    // 添加资源
    add() {
      this.axios
        .post("/admin/rbacm/add/resource", {
          rname: this.name,
          pid: this.radio,
          url: this.url,
          type: this.status,
        })
        .then((res) => {
          console.log("添加资源的响应:", res);
          if (res.data.code == 200) { 
            this.$message({
              type: "success",
              message: res.data.message,
            });
            this.name = "";
            this.url = "";
            this.dialogVisible = false;
            window.location.reload()
          } else {
            this.$message({
              type: "success",
              message: "添加失败",
            });
          }
        })
        .catch((err) => {
          console.log(">>>", err);
        });
    },
    urlPost() {
      this.status = 1;
      this.radio = "1";
    },
    post() {
      this.status = 2;
      this.radio = "0";
    },
  },

  computed: {},

  watch: {},

  directives: {},

  filters: {},
};
</script>

<style scoped >
* {
  margin: 0;
}
.top1 {
  width: 80%;
}
</style>
